package main

import (
	"fmt"
	"time"
)

// jobs 只能输出 int
// results 只能输入 int
func worker(id int, jobs <- chan int, results chan <- int) {
	// 从 chan 拿取任务
	for j := range jobs {
		fmt.Printf("worker: %d start job: %d\n", id, j)
		time.Sleep(time.Second) // 做任务
		fmt.Printf("worker:%d end job: %d\n", id, j)
		results <- j * 2

	}
}

func main() {
	jobs := make(chan int, 100)
	results := make(chan int, 100)
	for w := 1; w <= 3; w++ {
		// 初始化三个 worker
		go worker(w, jobs, results)
	}
	// 创建 5个任务
	// 任务一旦创建， worker就开始工作
	for j := 1; j <= 5; j++ {
		jobs <- j
	}
	close(jobs)
	//for a := range results{
	//	fmt.Println(a)
	//}
	for i := 1; i <= 5; i++ {
		msg := <- results
		fmt.Println(msg)
	}
	close(results)

}
